/**
 * 
 */
package com.vladimirjosu.fhc;

import com.vladimirjosu.util.Util;

/**
 * @author vjosu
 * 
 */
public class BalancedSmileys {

	private String mInput;

	public BalancedSmileys(String input) {
		mInput = input;
	}

	public String solve() {
		String newInput = mInput.replaceAll(":[^)(]", "");
		Util.log("\nAInput: " + mInput);
		Util.log("\nBInput: " + newInput + "\n");
		boolean balanced = isBalanced(newInput);
		return balanced ? "YES" : "NO";
	}

	private boolean isBalanced(String newInput) {
		int score = 0;
		int error = 0;
		for (int i = 0; i < newInput.length(); i++) {
			char c = newInput.charAt(i);
			switch (c) {
			case ':':
				error++;
				break;
			case '(':
				score++;
				break;
			case ')':
				score--;
				break;
			}
			if (score < 0 && Math.abs(score) > error) {
				return false;
			}
		}
		return Math.abs(score) <= error;
	}
}
